package com.kun.service.user;

import com.kun.dao.BaseDao;
import com.kun.dao.role.RoleDaoImpl;
import com.kun.dao.user.UserDao;
import com.kun.dao.user.UserDaoImpl;
import com.kun.entity.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserServiceImpl implements UserService {

    //业务层都会调用dao层，所以我们要引入Dao层
    private UserDao userDao;
    public UserServiceImpl() {
        this.userDao = new UserDaoImpl();
    }

    /**
     * 用户登录
     * @param userCode  用户代号码
     * @param password  用户密码
     * @return          查询到的用户信息
     */
    @Override
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;

        try{
            connection = BaseDao.getConnection();
            //通过业务层调用对应具体的数据库操作
            user = userDao.getLoginUser(connection,userCode);
            if(user != null && !user.getUserPassword().equals(password)){
                user = null;//密码不正确
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }

        return user;
    }

/*    @Test//测试登录
    public void loginTest(){
        UserServiceImpl s = new UserServiceImpl();
        User login = s.login("admin", "123456ry7");
        System.out.println(login.getUserPassword());
    }*/

    /**
     * 根据用户ID修改密码
     * @param id    用户ID
     * @param pwd   将要修改的密码
     * @return      修改成功返回true 失败返回false
     */
    @Override
    public boolean updatePwd(long id, String pwd) {
        Connection connection = null;
        boolean flag = false;

        //修改密码
        try {
            connection = BaseDao.getConnection();
            if(userDao.updatePwd(connection,id,pwd) > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }

        return flag;
    }

    //查询用户数（名称模糊查询+角色ID）
    @Override
    public int getUserCount(String userName, int userRole) {
        Connection connection = null;
        int count = 0;

        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection,userName,userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection);
        }

        return count;
    }

/*    @Test
    public void getUserCountTest(){
        UserServiceImpl userService = new UserServiceImpl();
        int count = userService.getUserCount(null, 0);
        System.out.println(count);
    }*/

    /**
     * 查询用户信息(根据用户名或者角色查询用户信息)
     * @param queryUserName 模糊查询的用户名
     * @param queryUserRole 用户所属角色号 0-->任意角色
     * @param currentPageNo 当前页码
     * @param pageSize      每页展示条数
     * @return
     */
    @Override
    public ArrayList<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        ArrayList<User> list = new ArrayList<User>();

        try{
            connection = BaseDao.getConnection();
            list = userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);
            RoleDaoImpl roleDao = new RoleDaoImpl();
            for (User user : list) {
                user.setUserRoleName(roleDao.getRoleName(connection,user.getUserRole()));
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection);
        }

        return list;
    }

    /*@Test
    public void getUserListTest(){
        ArrayList<User> userList = new UserServiceImpl().getUserList(null, 0, 1, 5);
        for(User u:userList){
            System.out.println(u.getUserName());
        }
    }*/

    //添加用户
    @Override
    public boolean addUser(User user){
        Connection connection = null;
        boolean flag = false;

        try{
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启事务
            int updateRow = userDao.addUser(connection,user);
            connection.commit();//提交事务
            if(updateRow > 0){//插入成功
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection);
        }

        return flag;
    }

    @Override
    public User getUserByID(Long id) {
        Connection connection = null;
        User user = null;
        if(id != null){
            connection = BaseDao.getConnection();
            try {
                user = userDao.getUser(connection,id,null);
                user.setUserRoleName(new RoleDaoImpl().getRoleName(connection,user.getUserRole()));
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection);
            }
        }
        return user;
    }

    @Override
    public User getUserByUserCode(String userCode) {
        Connection connection = null;
        User user = null;
        if(userCode != null){
            connection = BaseDao.getConnection();
            try {
                user = userDao.getUser(connection,null,userCode);
                user.setUserRoleName(new RoleDaoImpl().getRoleName(connection,user.getUserRole()));
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection);
            }
        }
        return user;
    }

    //删除一个用户（ID）
    @Override
    public boolean delUser(Long id){
        Connection connection = null;
        boolean flag = false;
        if(id != null){
            try {
                connection = BaseDao.getConnection();
                connection.setAutoCommit(false);//开启事务
                int delRows = userDao.delUser(connection,id,null);
                connection.commit();
                flag = delRows > 0;
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection);
            }
        }
        return flag;
    }

    //修改用户信息
    @Override
    public boolean modifyUser(User user){
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);
            flag = userDao.modifyUser(connection,user) > 0;
            connection.commit();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection);
        }
        return flag;
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();

        /*//添加用户
        User user = new User();
        user.setGender(1);
        user.setUserPassword("1");
        user.setUserCode("1");
        user.setAddress("sdgsd");
        user.setBirthday(new Date());
        user.setCreatedBy(1);
        user.setCreationDate(new Date());
        user.setUserRole(1);
        user.setPhone("111");
        user.setUserName("1");
        System.out.println(userService.addUser(user));*/

//        User userByUserCode = userService.getUserByUserCode("admin");
//        System.out.println(userByUserCode.getUserName());

//        User user = userService.getUserByID(1L);
//        System.out.println(user.getUserName());

//        userService.delUser(17L);
    }
}
